package com.ganzalang.gmall.learning.netty.nio.zerocopy;

import lombok.extern.slf4j.Slf4j;

import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.net.Socket;

/**
 * @description: 传统IO的客户端
 * @author: Ganzalang
 * @Date 2021/7/15 15:03
 */
@Slf4j
public class OldIOClient {
    public static void main(String[] args) throws Exception {
        Socket socket = new Socket("localhost", 7001);
        String fileName = "D:\\file01.txt";
        FileInputStream inputStream = new FileInputStream(fileName);
        DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());

        byte[] buffer = new byte[4096];
        long readCount;
        long total = 0;

        long start = System.currentTimeMillis();
        log.info("开始拷贝数据");
        while ((readCount = inputStream.read(buffer)) >= 0) {
            total += readCount;
            log.info("拷贝了{}个字节的数据，目前总数据为{}", readCount, total);
            dataOutputStream.write(buffer);
        }

        log.info("发送总字节数：" + total + ", 耗时：" + (System.currentTimeMillis() - start));
        dataOutputStream.close();
        socket.close();
        inputStream.close();

    }
}
